package com.lzy.product.apis.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzy.common.apis.PresentServiceApi;
import com.lzy.coupon.apis.CouponServiceApi;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.StringUtil;
import com.lzy.framework.tools.kv.KvClient;
import com.lzy.member.apis.MemberAccountServiceApi;
import com.lzy.member.apis.MemberCouponServiceApi;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.payment.apis.PayMemberAccountApi;
import com.lzy.product.apis.OrderProductServiceApi;
import com.lzy.product.apis.OrderProfitServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.profit.ProfitParamDto;
import com.lzy.product.service.OrderProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by WIN7 on 2017/11/29.
 */
@Service
public class OrderProductServiceApiImpl implements OrderProductServiceApi {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderProductServiceApiImpl.class);

    @Autowired
    OrderProductService orderProductService;
    @Autowired
    OrderProfitServiceApi orderProfitServiceApi;
    @Autowired
    MemberAccountServiceApi memberAccountServiceApi;
    @Autowired
    MemberUserServiceApi memberUserServiceApi;
    @Autowired
    PayMemberAccountApi payMemberAccountApi;
    @Autowired
    PresentServiceApi presentServiceApi;
    @Autowired
    CouponServiceApi couponServiceApi;
    @Autowired
    MemberCouponServiceApi memberCouponServiceApi;
    @Resource
    private KvClient kvClient;

    @Override
    public Long insert(OrderProductDto dto) {
        return orderProductService.insert(dto);
    }

    @Override
    public Boolean update(OrderProductDto dto) {
        return orderProductService.update(dto);
    }

    @Override
    public Boolean delete(Long id) {
        return orderProductService.delete(id);
    }

    @Override
    public Boolean remove(Long id) {
        return orderProductService.remove(id);
    }

    @Override
    public OrderProductDto findById(Long id) {
        return orderProductService.findById(id);
    }

    @Override
    public List<OrderProductDto> query(OrderProductDto dto) {
        return orderProductService.query(dto);
    }

    @Override
    public PageInfo<OrderProductDto> selectList(Pager pager, OrderProductDto dto) {
        return orderProductService.selectList(pager, dto);
    }

    @Override
    public List<OrderProductDto> selectListByClassifyId(OrderProductDto dto, Long classifyId) {
        return orderProductService.selectListByClassifyId(dto, classifyId);
    }

    @Override
    public Boolean calculateProfit(OrderProductDto orderDto) {
        ProfitParamDto profitParamDto = new ProfitParamDto();
        profitParamDto.setOrderId(orderDto.getOrderId());
        profitParamDto.setUserId(orderDto.getCreateUser());
        profitParamDto.setSkuCode(orderDto.getProductId());
        profitParamDto.setOrderTime(orderDto.getPayTime());
        profitParamDto.setOrderMoney(orderDto.getMoneyCount());
        String key = BusinessType.Cache.QUEUE_PRODUCT_PROFIT_RECORD + orderDto.getOrderId();
        String flag = kvClient.read(key, String.class);
        // 判断有没有执行过
        if (StringUtil.isNotBlank(flag) && BusinessType.Status.ONE.toString().equals(flag)) {
            // 订单已经分利，打印信息
            LOGGER.error("订单分利======失败======重复提交分利: " + JSON.toJSONString(profitParamDto));
            return false;
        }

        Boolean res = orderProfitServiceApi.calculate(profitParamDto);
        if (!res) {
            // 订单分利失败，打印信息
            LOGGER.error("订单分利失败：" + JSON.toJSONString(profitParamDto));
            return false;
        }
        kvClient.save(key, BusinessType.Status.ONE, BusinessType.EXPIRED_SECOND);

        return true;
    }

    @Override
    public PageInfo<OrderProductDto> selectMemberList(Pager pager, OrderProductDto orderProductDto) {
        return orderProductService.selectMemberList(pager, orderProductDto);
    }

    @Override
    public PageInfo<OrderProductDto> selectMemberListByCond(OrderProductDto orderProductDto) {
        return orderProductService.selectMemberListByCond(orderProductDto);
    }


    @Override
    public PageInfo<OrderProductDto> selectMemberOrderList(Pager pager, OrderProductDto orderProductDto) {
        return orderProductService.selectMemberOrderList(pager, orderProductDto);
    }

    @Override
    public Integer selectNumberByProductId(OrderProductDto dto) {
        return orderProductService.selectNumberByProductId(dto);
    }

    @Override
    public String selectSerialIdByMemberId(Long memberId) {
        List<OrderProductDto> orderProductDtoList = orderProductService.selectOrderProductListByMemberId(memberId);
        if(!orderProductDtoList.isEmpty() && orderProductDtoList.size() > 0){
            String serialId = "";
            OrderProductDto orderProductDto = orderProductDtoList.get(0);
            if(null != orderProductDto.getCityCode() && !"".equals(orderProductDto.getCityCode())){
                String createTime = "0000";
                Integer count = 0;
                if(null != orderProductDto.getCreateTime() && !"".equals(orderProductDto.getCreateTime())){
                    createTime = orderProductDto.getCreateTime().substring(2,7).replace("-","");
                    count = orderProductService.selectOrderProductCountByPayTime(orderProductDto.getCreateTime(),orderProductDto.getCityCode());
                }
                DecimalFormat df=new DecimalFormat("0000");
                String number=df.format(count);
                serialId = "No."+orderProductDto.getCityCode() + createTime + number;
                return serialId;
            }
        }
        return null;
    }
}
